package com.example.httpclient.FileUtils;

import android.content.Context;

import com.example.httpclient.uilts.CloseUtil;
import com.example.httpclient.uilts.printString;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class rwFile {

    public static void writePic(String name) {

    }


    public static InputStream readPic(String name) {

        return null;
    }

    public static void writeTxt(Context context, ArrayList<String> DataList,String dirname,String FileName) {
        FileUtils.CreateFile(dirname);
        FileWriter fw = null;
        BufferedWriter bw = null;
        boolean isOk = true;
        try {
            File file=new File(dirname+FileName);
            fw = new FileWriter(new File(dirname));
            bw = new BufferedWriter(fw);
            for (int i = 0; i < DataList.size(); i++) {
                String s=DataList.get(i);
                bw.write(s+"\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
            isOk = false;
            printString.print(context, "写入失败");
            CloseUtil.closeAll(bw, fw);
        } finally {
            CloseUtil.closeAll(bw, fw);
            if (isOk) {
                printString.print(context, "写入成功");
            }

        }
    }

    public static ArrayList<String> readTxt(Context context, String name) {
        BufferedReader br = null;
        InputStreamReader isr = null;
        ArrayList<String> words = new ArrayList<>();
        try {
            isr = new InputStreamReader(context.getResources().getAssets().open(name), "utf-8");
            br = new BufferedReader(isr);
            String s = "";
            while ((s = br.readLine()) != null) {
                words.add(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
            CloseUtil.closeAll(br, isr);
        } finally {
            CloseUtil.closeAll(br, isr);
            return words;
        }

    }

}
